package com.mvc.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * @author H
 * @date 2023/1/8
 */
@RestController
@RequestMapping("/file/upload")
public class TestFileUploadController {

    /**
     * 使用HttpServletRequest接收(使用Get请求和Post请求，用body体中media type = multipart/form-data都可以上传)
     * @param request
     * @return
     */
    @RequestMapping(value = "/request", method = {RequestMethod.GET, RequestMethod.POST})
    public String uploadRequest(HttpServletRequest request) {
        MultipartHttpServletRequest multipartHttpServletRequest;
        // 强制转换为MultipartHttpServletRequest接口对象
        if (request instanceof MultipartHttpServletRequest) {
            multipartHttpServletRequest = (MultipartHttpServletRequest) request;
        } else {
            return "fail";
        }
        // 获取MultipartFile文件信息
        MultipartFile multipartFile = multipartHttpServletRequest.getFile("file");
        // 获取源文件名称
        String originalFilename = multipartFile.getOriginalFilename();
        boolean exists = Files.exists(Paths.get("D:\\data", originalFilename));
        if (exists) {
            return "文件已存在";
        }
        File file = new File("D:\\data\\" + originalFilename);

        try {
            // 保存文件
            multipartFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
            return "fail";
        }

        return "success";
    }

    /**
     * 使用MultipartFile接收
     * @param file
     * @return
     */
    @RequestMapping(value = "/multipart", method = {RequestMethod.GET, RequestMethod.POST})
    public String uploadMultipart(MultipartFile file) {
        String name = file.getName();
        // 接收参数的参数名称 “file”
        System.out.println("name：" + name);
        // 上传文件的源文件名
        String originalFilename = file.getOriginalFilename();
        System.out.println("originalFilename：" + originalFilename);
        File dest = new File("D:\\data\\", originalFilename);
        System.out.println("上传绝对路径：" + dest.getAbsolutePath());
        try {
            // 保存文件
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
            return "fail";
        }

        return "success";
    }

    /**
     * 使用Part接收（推荐的方式）
     * @param file
     * @return
     */
    @RequestMapping(value = "/part", method = {RequestMethod.GET, RequestMethod.POST})
    public String uploadPart(Part file) {
        // 获取提交文件名称
        String fileName = file.getSubmittedFileName();
        System.out.println("提交的文件名：" + fileName);
        try {
            // 写入文件
            file.write("D:\\data\\" + fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }

        return "success";
    }
}
